﻿package andy.demo.gusture;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import andy.demo.R;

/**
 *  ACTION_DOWN, ACTION_MOVE, ACTION_UP, 和ACTION_CANCEL。<p>
 *  ACTION_DOWN是指按下触摸屏<p>
 *  ACTION_MOVE是指按下触摸屏后移动受力点，<p>
 *  ACTION_UP则是指松开触摸屏，<p>
 *  ACTION_CANCEL不会由用户直接触发（所以不在今天的讨论范围，请参考ViewGroup.onInterceptTouchEvent(MotionEvent)）<p>
 * @author BoyceAndy
 *
 */
public class GestureTest extends Activity implements OnTouchListener,
		OnGestureListener {

	private static final float FLING_MIN_DISTANCE = 200;
	private static final float FLING_MIN_VELOCITY = 300;

	
	 private GestureDetector gestureScanner;
	private TextView tv;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gesture_function);
		tv = (TextView) this.findViewById(R.id.tv_gesture);
		tv.setOnTouchListener(this);
		tv.setLongClickable(true);
		
		
		gestureScanner = new GestureDetector(this);
		
		gestureScanner.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener(){  
		      public boolean onDoubleTap(MotionEvent e) {  
		        tv.setText("-" + "onDoubleTap" + "-");  
		        //双击时产生一次
		        Log.v("test", "onDoubleTap");
		        return false;  
		      }
		      public boolean onDoubleTapEvent(MotionEvent e) {  
		        //双击时产生两次
		        Log.v("test", "onDoubleTapEvent");
		        return false;
		      }  
		      public boolean onSingleTapConfirmed(MotionEvent e) {  
		        tv.setText("-" + "onSingleTapConfirmed" + "-");  
		        //短快的点击算一次单击
		        Log.v("test", "onSingleTapConfirmed");
		        return false;  
		      }  
		    }); 



	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		System.out.println("onTouch------------------------------------");
		return gestureScanner.onTouchEvent(event);
	}

	
	// 用户轻触触摸屏，由1个MotionEvent ACTION_DOWN触发
	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		//Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
		System.out.println("onDown----------");
		return false;
	}

	// 用户轻触触摸屏，尚未松开或拖动，由一个1个MotionEvent ACTION_DOWN触发
	// 注意和onDown()的区别，强调的是没有松开或者拖动的状态
	@Override
	public void onShowPress(MotionEvent e) {
		//Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
		System.out.println("onShowPress----------");
	}

	// 用户（轻触触摸屏后）松开，由一个1个MotionEvent ACTION_UP触发
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		//Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
		System.out.println("onSingleTapUp----------");
		return false;
	}

	// 用户按下触摸屏、快速移动后松开，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		//Toast.makeText(this, "onFling", Toast.LENGTH_SHORT).show();
		System.out.println("onFling----------");
		// 参数解释：
		// e1：第1个ACTION_DOWN MotionEvent
		// e2：最后一个ACTION_MOVE MotionEvent
		// velocityX：X轴上的移动速度，像素/秒
		// velocityY：Y轴上的移动速度，像素/秒

		// 触发条件 ：
		// X轴的坐标位移大于FLING_MIN_DISTANCE，且移动速度大于FLING_MIN_VELOCITY个像素/秒
		
		
		if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
				&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
			// Fling left
			//Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();
		} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
				&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
			// Fling right
			//Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();
		}

		return false;
	}

	// 用户长按触摸屏，由多个MotionEvent ACTION_DOWN触发
	@Override
	public void onLongPress(MotionEvent e) {
		//Toast.makeText(this, "onLongPress", Toast.LENGTH_SHORT).show();
		System.out.println("onLongPress----------");

	}

	// 用户按下触摸屏，并拖动，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		System.out.println("onScroll----------");
		return false;
	}

}